
# 基于向量库做思考和action 
# 1、类比分析；
# 2、关联分析；
# 3、复杂问题分解子任务；
# 4、探索分析归纳与演绎推理：通过从具体实例中总结出一般规律（归纳），或根据已知的普遍规律推导出具体结论（演绎）。
# 系统思维：从整体上看待问题，分析各部分之间的相互关系，理解系统的动态行为。
# 假设检验：提出假设，通过实验或数据分析来验证或否定这些假设，以寻找问题的解决方案。
# 逆向思维：从问题的结果出发，推导出可能的原因和解决方案，常常能发现新的视角和思路。
#头脑风暴：通过集体讨论和创意激发，产生大量的想法和解决方案，随后再进行筛选和评估。
#敏捷方法：在复杂问题解决中，采用迭代和逐步推进的方式，根据反馈不断调整策略。
#决策树分析：通过构建决策树，清晰地展示不同选择的可能后果，帮助评估风险和收益。
#情景分析：考虑不同情境下可能出现的结果，通过构建多个情境模型来进行预测和计划。
#1. 结构性思维：将问题分解为不同的部分，分析它们之间的关系，从而更好地理解问题的本质。
# 2. 系统思维：把问题看作一个整体，考虑各个因素之间的相互作用和影响，以及系统的动态变化。
# 3. 归纳与演绎思维：通过归纳总结已有的经验和知识，推导出一般性的结论；或者从一般性的原理出发，演绎出具体的解决方案。
# 4. 逆向思维：从问题的相反方向进行思考，寻找新的解决途径。
# 5. 类比思维：将类似的问题或情况进行比较，借鉴已有的解决方案。
# 6. 创新思维：突破传统思维模式，提出新颖的想法和观点。
# 7. 批判性思维：对问题进行深入分析和评估，筛选出最有效的解决方案。
# 8. 可视化思维：利用图表、图像等方式将问题和解决方案直观地呈现出来，帮助理解和沟通。
# 9. 动态思维：考虑问题在不同时间和条件下的变化，制定灵活的解决方案。
# 10. 实验思维：通过尝试不同的方法和策略，探索解决问题的最佳途径。

from langchain_core.prompts import ChatPromptTemplate
from ApiBase import apiBase
from ApiModels import VectInfo,apiModels

from pydantic import BaseModel, Field
from typing import Annotated, List, Tuple
class Plan(BaseModel):
    """Plan to follow in future"""
    steps: List[str] = Field(
        description="different steps to follow, should be in sorted order"
    )
    
class ApiAct():
    def __init__(self,llm,vector):
        apiBase.getLLMConnect()
        self.llm=llm
        self.vector=vector
    
    # 生成计划
    def plan(self,vcts:list[VectInfo],topic):
        plans=self.vector.clts_query(vcts, topic,'act-plan')
        plan=""
        for p in plans:
            plan+=p['answer']
        system = f"""You are an expert in planning and decomposing this task with maximum accuracy.
For the following Task, make plans that can solve the problem STEP BY STEP. 
For each plan, indicate which external tool together with tool input to retrieve evidence. 
You can store the evidence into a variable #E that can be called by later tools. 
(Plan, #E1, Plan, #E2, Plan, ...)
## EXAMPLES
{plan}
## Begin!
describe your plans with rich details. Each Plan should be followed by only one #E.
"""
        template = ChatPromptTemplate.from_messages(
            [
                ("system", system),
                (
                    "human","Here is the Task: \n\n {question} \nRefer to the EXAMPLES above,make plans.",
                ),
            ]
        )
        question = template | self.llm 
        result=question.invoke({"question": topic})
        return result.content
    
    def act_related(self,vcts:list[VectInfo],topic):
        plans=self.vector.clts_query(vcts, topic,'act-relate')
        plan=""
        for p in plans:
            plan+=p['answer']
        system = f"""Given a user task, generate subtask task and solve them with maximum relevance.
For the following Task, make similar subtasks that can solve the task STEP BY STEP. 
For each subtask, indicate which external tool together with tool input to retrieve evidence. 
You can store the evidence into a variable #E that can be called by later tools. 
(Subtask, #E1, Subtask, #E2, Subtask, ...)
## EXAMPLES
{plan}
## Begin!
Describe your subtasks with rich details. Each Subtask should be followed by only one #E.
"""
        template = ChatPromptTemplate.from_messages(
            [
                ("system", system),
                (
                    "human","Refer to the EXAMPLES above,generate a comma separated list of between 2 and 5 subtasks related to: {question}.",
                ),
            ]
        )
        question = template | self.llm 
        result=question.invoke({"question": topic})
        return result.content
    # 正向分拆子任务
    def map(self,vcts:list[VectInfo],topic):
        plans=self.vector.clts_query(vcts, topic,'act-map')
        plan=""
        for p in plans:
            plan+=p['answer']
        system =f"""You are an expert in task decomposition, decomposing this task with maximum accuracy.
For the following Task, you can solve the task STEP BY STEP. 
For each subtask, indicate which external tool together with tool input to retrieve evidence. 
You can store the evidence into a variable #E that can be called by later tools. 
(Subtask, #E1, Subtask, #E2, Subtask, ...)
## EXAMPLE
{plan}
## Begin!
Describe your subtasks with rich details. Each Subtask should be followed by only one #E.
"""
        template = ChatPromptTemplate.from_messages(
            [
                ("system", system),
                (
                    "human","Here is the initial Task: \n\n {question} \nRefer to the EXAMPLES above,decomposing this task.",
                ),
            ]
        )
        question = template | self.llm
        result=question.invoke({"question": topic})
        return result.content
    # 逆向分解
    def reverse(self,vcts:list[VectInfo],topic):
        return ""
    # 重写任务
    def rewriter(self,vcts:list[VectInfo],topic):
        plans=self.vector.clts_query(vcts, topic,'act-wt')
        plan=""
        for p in plans:
            plan+=p['answer']
        system = f"""You are a question rewriter that can transform or decompose input questions into multiple better versions optimized for vector library retrieval. 
View input and attempt to infer potential semantic intent/meaning.
For the following Question, make sub-question that can solve the problem STEP BY STEP. 
For each sub question, indicate which external tool together with tool input to retrieve evidence.
You can store the evidence into a variable #E that can be called by later tools. 
(Subquestion, #E1, Subquestion, #E2, Subquestion, ...)
## EXAMPLES
{plan}
## Begin!
Describe your Subquestions with rich details. Each Subquestion should be followed by only one #E.
"""
        template = ChatPromptTemplate.from_messages(
            [
                ("system", system),
                (
                    "human","Here is the initial Question: \n\n {question} \nRefer to the EXAMPLES above,Formulate an improved question.",
                ),
            ]
        )
        question = template | self.llm 
        result=question.invoke({"question": topic})
        return result.content